//app.js
const api = require('utils/httpUtil.js');
App({
  onLaunch: function () {

    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    var that = this;
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        api.httpRequest(api.getOpenIdAPI, {
            code: res.code
          })
          .then(res => {
            if (res.code == 200) {
              that.globalData.openId = res.data.openid;
              // that.globalData.openId = "ojNkK7elJbD6QzoKXmwRwMc4s3Js";
              //判断用户是否注册
              api.httpRequest(api.authAPI, {
                 "openId": res.data.openid
                  // "openId":"ojNkK7elJbD6QzoKXmwRwMc4s3Js"
                })
                .then(res => {
                  if (res.code == 200) {
                    //判断用户是否注册
                    if (res.msg == "会员" || res.msg == "物业人员") {
                      that.globalData.userInfo = res.data;
                      that.globalData.userType = "member";
                      wx.redirectTo({
                        url: '/pages/member/index/index',
                      })
                    } else {
                      wx.redirectTo({
                        url: '/pages/visitor/index/index',
                      })
                    }
                  } else {
                    wx.redirectTo({
                      url: '/pages/visitor/index/index',
                    })
                  }
                })
                .catch(error => {
                  wx.showToast({
                    title: error.message,
                    duration: 5000,
                    mask: true,
                    icon: 'none'
                  })
                });

            }
          })
          .catch(error => {
            wx.showToast({
              title: error.message,
              duration: 1000,
              mask: true,
              icon: 'none'
            })
          });


        this.globalData.msgCount = 0;
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              // this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
    // 获取系统状态栏信息
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let capsule = wx.getMenuButtonBoundingClientRect();
        if (capsule) {
          this.globalData.Custom = capsule;
          this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
        } else {
          this.globalData.CustomBar = e.statusBarHeight + 50;
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    openId: null,
    msgCount: 0,
  },
})